package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BusinessException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.IDishService;
import com.itheima.reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ICategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private IDishService iDishService;
    @Autowired
    private ISetmealService iSetmealService;

    @Override
    public void remove(Long id) {

        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper();
        LambdaQueryWrapper<Dish> eq = lambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count = iDishService.count(eq);
        if (count>0){
            throw new BusinessException("该分类下有菜品，不能删除");
        }


        LambdaQueryWrapper<Setmeal> la= new LambdaQueryWrapper();
        LambdaQueryWrapper<Setmeal> e = la.eq(Setmeal::getCategoryId, id);
        int count1 = iSetmealService.count(e);
        if (count1>0){
            throw new BusinessException("该分类下有套餐，不能删除");

        }

        super.removeById(id);

    }
}
